﻿namespace h38
{
    public class Solution {
        public int FindCircleNum(int[][] isConnected) {
            var visited = new bool[isConnected.Length];
            int res = 0;
            for(int i=0;i<isConnected.Length;++i){
                if(!visited[i]){
                    ++res;      
                    dfs(visited,i,isConnected);      
                }
           
            }
            return res;
        }

        public void dfs(bool[] visited,int city,int[][] isConnected){
            for (int i = 0; i < isConnected.Length; ++i){
                if (!visited[i] && isConnected[i][city] == 1){
                    visited[city] = true;
                    dfs(visited, i, isConnected);
                }
            }
        }
    }
}